<?php

namespace App\Console\Commands;

use App\Models\PddOrderModel;
use Illuminate\Console\Command;
use NiuGengYun\EasyTBK\Factory;
use NiuGengYun\EasyTBK\pinduoduo\request\DdkOrderListIncrementGetRequest;

/**
 * Class SyncPddOrders
 * @package App\Console\Commands
 * 拉取拼多多订单，一分钟一次
 */
class SyncPddOrders extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sync:pddOrders';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '同步拼多多订单';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $pdd = Factory::pinduoduo();
        $req = new DdkOrderListIncrementGetRequest();
        $startTime = strtotime(date("Y-m-d 00:00:00"));
        $endTime = time();
        //$endTime = strtotime(date("Y-m-26 23:59:59"));
        $req->setStartUpdateTime($startTime);
        $req->setEndUpdateTime($endTime);
        $res = $pdd->execute($req);
        $orderList = $res['order_list_get_response']['order_list'];
        foreach ($orderList as $order) {
            $pddOrderModel = new PddOrderModel();
            $orderInfo = $pddOrderModel->where([
                'order_sn' => $order['order_sn']
            ])->first();
            if (!empty($orderInfo)) {
                $orderInfo->order_status = $order['order_status'];
                $orderInfo->order_status_desc = $order['order_status_desc'];
                $orderInfo->fail_reason = $order['fail_reason'];
                $orderInfo->save();
            } else {
                $pddOrderModel->goods_id = $order['goods_id'];
                $pddOrderModel->goods_name = $order['goods_name'];
                $pddOrderModel->goods_price = $order['goods_price'];
                $pddOrderModel->custom_parameters = $order['custom_parameters'];
                $pddOrderModel->order_amount = $order['order_amount'];
                $pddOrderModel->order_pay_time = $order['order_pay_time'];
                $pddOrderModel->order_sn = $order['order_sn'];
                $pddOrderModel->order_status = $order['order_status'];
                $pddOrderModel->fail_reason = $order['fail_reason'] ?? "";
                $pddOrderModel->order_status_desc = $order['order_status_desc'];
                $pddOrderModel->p_id = $order['p_id'];
                $pddOrderModel->promotion_amount = $order['promotion_amount'];
                $pddOrderModel->promotion_rate = $order['promotion_rate'];
                $customParametersData = json_decode($order['custom_parameters'], true);
                $pddOrderModel->member_id = $customParametersData['memberId'] ?? 0;
                $pddOrderModel->goods_thumbnail_url = $order['goods_thumbnail_url'];
                $pddOrderModel->order_create_time = $order['order_create_time'];
                $pddOrderModel->save();
            }
        }
    }
}
